package com.example.demo.outlook.controller;


import com.azure.core.annotation.Get;
import com.example.demo.outlook.common.R;
import com.example.demo.outlook.demo.OssJavaSdkQuickStart;
import com.example.demo.outlook.mapper.UserMapper;
import com.example.demo.outlook.pojo.User;
import com.example.demo.outlook.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/HelloIndex")
@CrossOrigin(origins = "*")
public class HelloIndexController {


    private final String key ="user_id";
    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private OssJavaSdkQuickStart ossJavaSdkQuickStart;

    @GetMapping("/user/item/{id}")
    @ResponseBody
    public String HelloWorld(@PathVariable String id) {
        System.out.println(id);

        return "Hello " + id;
    }

    @GetMapping("/user/{userId}")
    @ResponseBody
    public R<User> getUserByUserId(@PathVariable Integer userId) {
        System.out.println(userId);

        if(redisUtil.get(key) !=null) {
            return R.ok((User) redisUtil.get(key) );
        }
        User user = userMapper.selectUserId(userId);

        redisUtil.set(key,user);
        return R.ok(user);
    }

    @GetMapping("/user")
    @ResponseBody
    public User getUserById(@Param("id") Integer id) {


        User user = new User();

        user.setDeptId(1);

        return user;

    }

    @GetMapping("/user/all")
    @ResponseBody
    public List<User> getAllUser() {


        System.out.println("hello inde");

        List<User> allListUser = userMapper.selectAll(null,null);

        System.out.println(allListUser);
        return allListUser;
    }

    @DeleteMapping("/user/{userId}")
    public R<String> deleteUserById(@PathVariable Integer userId ) {

        System.out.println(userId);

        return R.ok("删除成功");
    }

    @GetMapping("/fileUpload")
    public R<String> fileUpload() {

        System.out.println("oss 创建中");

        try {
            OssJavaSdkQuickStart.fileUpload();

          return   R.ok("文件测试成功");
        }catch (Exception e) {

            System.err.println(e.getMessage());

         return    R.error("文件测试失败");
        }
    }


    @PostMapping("/fileUpload")
    public R<String> fileUploadByPost(@RequestBody MultipartFile file) throws IOException {

        System.out.println("oss 创建中");

        System.out.println(file.getContentType());

        System.out.println(file.getBytes());

        System.out.println(file.getOriginalFilename());

        try {
            OssJavaSdkQuickStart.fileUp(file);

            return   R.ok("文件测试成功");
        }catch (Exception e) {

            System.err.println(e.getMessage());

            return    R.error("文件测试失败");
        }
    }
}
